package com.app.community.service;

import com.app.community.dto.NotificationDTO;
import com.app.community.enums.NotificationStatusEnum;
import com.app.community.enums.NotificationTypeEnum;
import com.app.community.mapper.NotificationMapper;
import com.app.community.model.entity.Notification;
import com.app.community.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * created by czy on 2020/11/20 9:30
 **/
@Service
public class NotificationService {

    @Autowired
    NotificationMapper notificationMapper;

    /**
     * 插入点赞通知
     */
    public void insertLikeNotification(Long questionId, String notifier) {
        notificationMapper.insertNotification(questionId, notifier, NotificationTypeEnum.LIKE.getType(), NotificationStatusEnum.UNREAD.getStatus(), TimeUtils.getTime());
    }

    /**
     * 插入评论通知
     */
    public void insertCommentNotification(Long questionId, String notifier) {
        notificationMapper.insertNotification(questionId, notifier, NotificationTypeEnum.COMMENT.getType(), NotificationStatusEnum.UNREAD.getStatus(), TimeUtils.getTime());
    }

    /**
     * 更新通知为已读
     */
    public void updateNotificationStatus(String receiver) {
        notificationMapper.updateNotificationStatus(NotificationStatusEnum.READ.getStatus(), receiver);
    }

    /**
     * 删除通知，只允许删除未读的信息,目前只允许删除点赞
     */
    public void deleteNotification(Long questionId, String notifier) {
        notificationMapper.deleteNotification(questionId, notifier, NotificationStatusEnum.UNREAD.getStatus(), NotificationTypeEnum.LIKE.getType());
    }

    /**
     * 查询某用户的某种通知
     */
    public List<NotificationDTO> selectNotification(String receiver, Integer type) {
        return notificationMapper.selectNotification(receiver, type, NotificationStatusEnum.UNREAD.getStatus());
    }

    /**
     * 统计用户未读消息
     */
    public Integer countUNREADNotification(String receiver){
        return notificationMapper.countUNREADNotification(receiver,NotificationStatusEnum.UNREAD.getStatus());
    }
}
